package com.longchen.mall.config.exception;

import cn.dev33.satoken.exception.NotLoginException;
import com.longchen.mall.common.Result;
import com.longchen.mall.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author xbfinal
 * @Package com.longcehng.mall.config.exception
 * @Date 2024/7/14 10:11
 * 描述 : 全局异常
 */

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //全局异常处理
    @ResponseBody//返回json数据
    @ExceptionHandler(Exception.class)
    public Result error(Exception e){
        log.info("当前异常信息:{} \n",e.getMessage());
        log.info("具体信息：",e);
        return Result.failed(e.getMessage());
    }
    //全局异常处理 用于satoken的未登录的异常
    @ResponseBody//返回json数据
    @ExceptionHandler(NotLoginException.class)
    public Result errorNotLogin(NotLoginException e){
        log.info("当前异常信息:{} \n",e.getMessage());
        log.info("具体信息：",e);
        return Result.failed(e.getMessage()).addCode(ResultCode.LONGIN_ERROR);
    }

    @ResponseBody
    @ExceptionHandler(MallException.class)  //自定义的异常处理方法
    public Result divError(MallException e){
        log.info("当前异常信息:{} ",e.getMsg());
        return Result.failed(e.getMsg()).addCode(e.getCode());
    }


}

